// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.cdc;

import org.dengues.commons.DenguesCommonsPlugin;
import org.dengues.commons.ui.swt.colorstyledtext.ColorManager;
import org.dengues.commons.ui.swt.colorstyledtext.ColorStyledText;
import org.dengues.commons.ui.swt.colorstyledtext.EModeLanguage;
import org.dengues.core.DenguesCorePlugin;
import org.dengues.core.connectivity.IConnectivityProperty;
import org.dengues.core.metadata.MetadataHelper;
import org.dengues.model.database.DBTable;
import org.dengues.model.project.ConnPropertySetType;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-5-5 qiang.zhang $
 * 
 */
public class ExecuteCDCSQLDialog extends Dialog {

    private ColorStyledText colorText;

    private ChangeDataCaptureManager manager;

    private String sql;

    private boolean isExec = false;

    private final boolean showTitle;

    private DBTable table;

    private String dtoW;

    /**
     * Qiang.Zhang.Adolf@gmail.com CreateCDCDialog constructor comment.
     * 
     * @param parentShell
     * @param manager
     * @param sql
     */
    public ExecuteCDCSQLDialog(Shell parentShell, ChangeDataCaptureManager manager, String sql, boolean showTitle) {
        super(parentShell);
        setShellStyle(getShellStyle() | SWT.MAX | SWT.MIN | SWT.RESIZE | SWT.APPLICATION_MODAL);
        this.manager = manager;
        this.sql = sql;
        this.showTitle = showTitle;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com CreateCDCDialog constructor comment.
     * 
     * @param shell
     * @param manager2
     * @param sql2
     */
    public ExecuteCDCSQLDialog(Shell shell, ChangeDataCaptureManager manager2, String[] sql2, boolean showTitle) {
        this(shell, manager2, sql2[0] + ChangeDataCaptureManager.SQL_SEQ + sql2[1] + ChangeDataCaptureManager.SQL_SEQ + sql2[2]
                + ChangeDataCaptureManager.SQL_SEQ + sql2[3], showTitle);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        Composite createDialogArea = (Composite) super.createDialogArea(parent);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        if (showTitle) {
            Composite titleComp = new Composite(createDialogArea, SWT.NONE);
            titleComp.setLayout(new GridLayout(4, false));
            titleComp.setLayoutData(gd);
            Label label = new Label(titleComp, SWT.NONE);
            label.setText("Select CDC: ");
            final Text text = new Text(titleComp, SWT.BORDER);
            text.setText(manager.getConnId());
            text.setEditable(false);
            gd = new GridData(GridData.FILL_HORIZONTAL);
            text.setLayoutData(gd);
            Button button = new Button(titleComp, SWT.PUSH);
            button.setText("...");
            button.addSelectionListener(new SelectionAdapter() {

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
                 */
                @Override
                public void widgetSelected(SelectionEvent e) {
                    DenguesCorePlugin default1 = DenguesCorePlugin.getDefault();
                    IConnectivityProperty res = default1.getWarehousePluginService().openSelectConnSetDialog(true);
                    if (res != null) {
                        ConnPropertySetType setType = MetadataHelper.getConnSetType(res);
                        manager = new ChangeDataCaptureManager(setType);
                        text.setText(manager.getConnId());
                        if (table != null && dtoW != null) {
                            String[] gsql = manager.genCreateCDCSQL(table, dtoW);
                            sql = gsql[0] + ChangeDataCaptureManager.SQL_SEQ + gsql[1] + ChangeDataCaptureManager.SQL_SEQ
                                    + gsql[2] + ChangeDataCaptureManager.SQL_SEQ + gsql[3];
                            colorText.setText(sql.replaceAll(ChangeDataCaptureManager.SQL_SEQ, ";\n"));
                        }
                    }
                }
            });
            button = new Button(titleComp, SWT.PUSH);
            button.setText("CDC Settings");
            button.addSelectionListener(new SelectionAdapter() {

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
                 */
                @Override
                public void widgetSelected(SelectionEvent e) {
                    new OpenCDCSetPrefsAction().run();
                }
            });
        }
        ColorManager colorManager = new ColorManager(DenguesCommonsPlugin.getDefault().getPreferenceStore());
        colorText = new ColorStyledText(createDialogArea, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, colorManager,
                EModeLanguage.SQL.getLanguageMode());
        Font font = new Font(parent.getDisplay(), "courier", 10, SWT.NONE); //$NON-NLS-1$
        colorText.setFont(font);
        colorText.setText(sql.replaceAll(ChangeDataCaptureManager.SQL_SEQ, ";\n"));
        colorText.setEditable(false);

        gd = new GridData(GridData.FILL_BOTH);
        colorText.setLayoutData(gd);

        Composite composite = new Composite(createDialogArea, SWT.CENTER);
        composite.setLayout(new GridLayout(2, false));
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalAlignment = GridData.CENTER;
        composite.setLayoutData(gd);
        Button exeBtn = new Button(composite, SWT.PUSH);
        exeBtn.setText("Execute");
        exeBtn.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                isExec = manager.exeSQLScript(sql);
            }
        });
        Button closeBtn = new Button(composite, SWT.PUSH);
        closeBtn.setText("Close");
        closeBtn.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                cancelPressed();
            }
        });
        return createDialogArea;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected void createButtonsForButtonBar(Composite parent) {
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
     */
    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText("Create Subscriber and Execute SQL Script");
        newShell.setSize(600, 400);
    }

    /**
     * Getter for isExec.
     * 
     * @return the isExec
     */
    public boolean isExec() {
        return this.isExec;
    }

    /**
     * Sets the table.
     * 
     * @param table the table to set
     */
    public void setTable(DBTable table) {
        this.table = table;
    }

    /**
     * Sets the dtoW.
     * 
     * @param dtoW the dtoW to set
     */
    public void setDtoW(String dtoW) {
        this.dtoW = dtoW;
    }
}
